package com.renwei.yunlong.activity.contacts;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.CommonSearchActivity;
import com.renwei.yunlong.activity.news.TranspaintActivity;
import com.renwei.yunlong.adapter.FriendCompanyAdapter;
import com.renwei.yunlong.adapter.StartChatListAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.EnterpriseBean;
import com.renwei.yunlong.bean.EnterpriseOwnerBean;
import com.renwei.yunlong.bean.EnterpriseServiceBean;
import com.renwei.yunlong.event.ChatListCloseEvent;
import com.renwei.yunlong.event.ContactCheckEvent;
import com.renwei.yunlong.global.AppHelper;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.UtilList;
import com.renwei.yunlong.view.SearchImage;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;


/**
 * 好友企业--页面
 * <p>
 * Created by Administrator on 2017/8/30.
 */

public class SeeContactsByEnterpriseActivity extends BaseActivity implements View.OnClickListener, XRecyclerView.LoadingListener, HttpTaskListener {

    private static int from_type = 0;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_edit)
    TextView tvEdit;
    @Bind(R.id.iv_cont)
    ImageView ivCont;
    @Bind(R.id.iv_edit)
    ImageView ivEdit;
    @Bind(R.id.iv_contacts)
    ImageView ivContacts;
    @Bind(R.id.iv_more)
    ImageView ivMore;
    @Bind(R.id.iv_scroll)
    ImageView ivScroll;
    @Bind(R.id.top_bar)
    RelativeLayout topBar;
    @Bind(R.id.search_image)
    SearchImage searchImage;
    @Bind(R.id.recyclerView)
    XRecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;
    @Bind(R.id.btn_commit)
    Button btnCommit;
    @Bind(R.id.rlv_bottom_contact)
    RecyclerView rlvBottomContact;
    @Bind(R.id.pannel_bottom)
    RelativeLayout pannelBottom;
    private StartChatListAdapter bottomListAdapter;
    private int page = 1;
    private FriendCompanyAdapter adapter;
    private String thisCompanyType;

    public static void OpenActivity(Context context, int from_type) {
        Intent intent = new Intent(context, SeeContactsByEnterpriseActivity.class);
        intent.putExtra("from_type", from_type);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_see_contact_by_enterprise);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        EventBus.getDefault().register(this);
        from_type = getIntent().getIntExtra("from_type", 0);
        if (from_type == 0) {
            pannelBottom.setVisibility(View.GONE);
        } else {
            pannelBottom.setVisibility(View.VISIBLE);
            btnCommit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    TranspaintActivity.openActivity(SeeContactsByEnterpriseActivity.this);

                  /*  EventBus.getDefault().post(new CommitMemberEvent(CommitMemberEvent.COMMIT_MEMBER_EVENT));*/
                }
            });
        }
        initData();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        ivEdit.setVisibility(View.GONE);
        tvTitle.setText("按好友企业查看");
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setLoadingListener(this);
        thisCompanyType = "1".equals(companyType) ? "2" : "1";
        adapter = new FriendCompanyAdapter(mContext, from_type, thisCompanyType);
        recyclerView.setAdapter(adapter);
        searchImage.setOnClickListener(this);
        rlvBottomContact.setLayoutManager(new LinearLayoutManager(this, LinearLayout.HORIZONTAL, false));
        bottomListAdapter = new StartChatListAdapter(this, companyType);
        rlvBottomContact.setAdapter(bottomListAdapter);
        initBottomContactList();
        netWork();
    }

    private void netWork() {
        String json = "";
        if ("1".equals(companyType)) {
            json = new Gson().toJson(new EnterpriseOwnerBean(ownerBean.getRows().getOwnerCode(), "", page, 20));
        } else if ("2".equals(companyType)) {
            json = new Gson().toJson(new EnterpriseServiceBean(serviceLoginBean.getRows().getServiceProviderCode(),
                    "", page, 20));
        }
        ServiceRequestManager manager = ServiceRequestManager.getManager();
        manager.queryFriendCompany(this, json, this);
    }


    private void setData(List<EnterpriseBean.RowsBean> rows) {
        if (rows != null && rows.size() < 20) {
            recyclerView.setLoadingMoreEnabled(false);
        }
        adapter.addAll(rows);
        if (adapter.getItemCount() == 0) {
            stateLayout.showEmptyView();
        } else {
            stateLayout.showContentView();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.search_image:
                CommonSearchActivity.OpenActivityFromContact(this, v, SeeContactsByEnterpriseActivity.class, 998, from_type, thisCompanyType);
                break;
                default:
                break;


        }
    }


    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onRefreshName(ContactCheckEvent event) {
        initBottomContactList();

    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onClosePage(ChatListCloseEvent event) {
        finish();
    }


    public void initBottomContactList() {
        bottomListAdapter.setData(AppHelper.getCheckContactList());
        String commitText = "";
        if (UtilList.getCount(AppHelper.getCheckContactList()) == 0) {
            commitText = "确定";
        } else {
            commitText = "确定(" + UtilList.getCount(AppHelper.getCheckContactList()) + ")";
        }
        btnCommit.setText(commitText);
    }


    @Override
    public void onRefresh() {
        recyclerView.setLoadingMoreEnabled(true);
        page = 1;
        adapter.clean();
        netWork();
    }

    @Override
    public void onLoadMore() {
        page++;
        netWork();
    }


    @Override
    public void onSuccess(int requestId, String result) {
        recyclerView.loadMoreComplete();
        recyclerView.refreshComplete();
        EnterpriseBean bean = new Gson().fromJson(result, EnterpriseBean.class);
        int code = bean.getMessage().getCode();
        if (code == 200) {
            setData(bean.getRows());
        } else {
            if (code == 1004) {
                stateLayout.showEmptyView();
            }
        }
    }

    @Override
    public void onException(int requestId, String code) {
        recyclerView.loadMoreComplete();
        recyclerView.refreshComplete();
        page--;
    }


}
